/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.example.gateway.handler;

import cn.hutool.core.util.StrUtil;
import com.example.common.base.constant.RedisKeyConstant;
import com.example.common.i18n.api.AjaxJson;
import com.example.common.i18n.util.MessageUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @author lengleng
 * @date 2018/7/5
 * 多语言切换
 */
@SuppressWarnings("NullableProblems")
@Slf4j
@Component
@AllArgsConstructor
public class LanguageHandler implements HandlerFunction<ServerResponse> {
	private final RedisTemplate redisTemplate;

	private static final String STARTWITH = "bearer";

	@Override
	public Mono<ServerResponse> handle(ServerRequest serverRequest) {
		//保存验证码信息
		String randomStr = serverRequest.queryParam("randomStr").get();
		String language= serverRequest.queryParam("lang").get();
		String header= serverRequest.headers().header("Authorization").toString();
		if(StrUtil.isBlank(language)){
			return ServerResponse
				.status(HttpStatus.OK)
				.contentType(MediaType.APPLICATION_JSON_UTF8)
				.body(BodyInserters.fromObject((MessageUtils.error("修改失败"))));
		}

		if (header != null && header.startsWith(STARTWITH)) {
			String accessToken = header.replace(STARTWITH, "").trim();
			redisTemplate.opsForValue().set(RedisKeyConstant.LANGUAGE_KEY + accessToken, language, 3600, TimeUnit.SECONDS);
		}else {
			if(!StrUtil.isBlank(randomStr)){
				redisTemplate.opsForValue().set(RedisKeyConstant.LANGUAGE_KEY + randomStr, language, 3600, TimeUnit.SECONDS);
			}else{
				//修改失败
			}
		}
		return ServerResponse
			.status(HttpStatus.OK)
			.contentType(MediaType.APPLICATION_JSON_UTF8)
			.body(BodyInserters.fromObject((MessageUtils.success(0,"修改成功",null))));
	}
}
